package com.zenithmind.user.pojo.domain;

import com.zenithmind.common.base.BaseEntity;
import com.zenithmind.user.pojo.domain.valueobject.UserProfile;
import com.zenithmind.user.pojo.domain.valueobject.UserStatus;
import com.zenithmind.user.pojo.domain.valueobject.StorageQuota;
import com.zenithmind.user.pojo.domain.valueobject.LoginInfo;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import io.swagger.v3.oas.annotations.media.Schema;
import com.baomidou.mybatisplus.annotation.*;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 用户聚合根
 * 遵循单一职责原则：只负责用户核心信息的管理
 * 遵循封装原则：通过值对象封装复杂属性
 */
@EqualsAndHashCode(callSuper = true)
@Getter
@NoArgsConstructor
@Schema(name = "用户实体", description = "用户聚合根")
@TableName("user")
public class User extends BaseEntity {

    @Schema(description = "用户名")
    private String username;

    @Schema(description = "密码哈希")
    private String password;

    @Schema(description = "昵称")
    private String nickname;

    @Schema(description = "头像URL")
    private String avatar;

    @Schema(description = "邮箱")
    private String email;

    @Schema(description = "手机号")
    private String mobile;

    @Schema(description = "状态代码")
    private Integer status;

    @Schema(description = "最后登录时间")
    private LocalDateTime lastLoginTime;

    @Schema(description = "最后登录IP")
    private String lastLoginIp;

    @Schema(description = "个人简介")
    private String intro;

    @Schema(description = "已用存储空间")
    private Long storageUsed;

    @Schema(description = "存储空间限制")
    private Long storageLimit;

    /**
     * 构造函数 - 创建新用户
     */
    public User(String username, String password, String email) {
        this.username = Objects.requireNonNull(username, "用户名不能为空");
        this.password = Objects.requireNonNull(password, "密码不能为空");
        this.email = Objects.requireNonNull(email, "邮箱不能为空");
        this.status = 1; // 默认启用
        this.storageUsed = 0L;
        this.storageLimit = 1024L * 1024L * 1024L; // 1GB

        validateUsername(username);
        validateEmail(email);
    }

    /**
     * 更新用户档案
     */
    public void updateProfile(String nickname, String avatar, String mobile, String intro) {
        this.nickname = nickname;
        this.avatar = avatar;
        this.mobile = mobile;
        this.intro = intro;
    }

    /**
     * 更新密码
     */
    public void updatePassword(String newPassword) {
        this.password = Objects.requireNonNull(newPassword, "密码不能为空");
    }

    /**
     * 启用用户
     */
    public void enable() {
        this.status = 1;
    }

    /**
     * 禁用用户
     */
    public void disable() {
        this.status = 0;
    }

    /**
     * 记录登录信息
     */
    public void recordLogin(String ipAddress) {
        this.lastLoginTime = LocalDateTime.now();
        this.lastLoginIp = ipAddress;
    }

    /**
     * 更新存储使用量
     */
    public boolean updateStorageUsed(long deltaSize) {
        long newUsed = this.storageUsed + deltaSize;
        if (newUsed < 0) {
            return false;
        }
        if (this.storageLimit != null && this.storageLimit > 0 && newUsed > this.storageLimit) {
            return false;
        }
        this.storageUsed = newUsed;
        return true;
    }

    /**
     * 检查是否可以使用指定大小的存储空间
     */
    public boolean canUseStorage(long size) {
        if (this.storageLimit == null || this.storageLimit <= 0) {
            return true;
        }
        return this.storageUsed + size <= this.storageLimit;
    }

    /**
     * 验证用户名格式
     */
    private void validateUsername(String username) {
        if (username == null || username.trim().isEmpty()) {
            throw new IllegalArgumentException("用户名不能为空");
        }
        if (username.length() < 3 || username.length() > 50) {
            throw new IllegalArgumentException("用户名长度必须在3到50之间");
        }
    }

    /**
     * 验证邮箱格式
     */
    private void validateEmail(String email) {
        if (email == null || email.trim().isEmpty()) {
            throw new IllegalArgumentException("邮箱不能为空");
        }
        if (!email.matches("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$")) {
            throw new IllegalArgumentException("邮箱格式不正确");
        }
    }

    /**
     * 检查用户是否处于活跃状态
     */
    public boolean isActive() {
        return this.status != null && this.status == 1;
    }

    // MyBatis-Plus需要的setter方法（仅用于数据库映射）
    public void setUsername(String username) { this.username = username; }
    public void setPassword(String password) { this.password = password; }
    public void setNickname(String nickname) { this.nickname = nickname; }
    public void setAvatar(String avatar) { this.avatar = avatar; }
    public void setEmail(String email) { this.email = email; }
    public void setMobile(String mobile) { this.mobile = mobile; }
    public void setStatus(Integer status) { this.status = status; }
    public void setLastLoginTime(LocalDateTime lastLoginTime) { this.lastLoginTime = lastLoginTime; }
    public void setLastLoginIp(String lastLoginIp) { this.lastLoginIp = lastLoginIp; }
    public void setIntro(String intro) { this.intro = intro; }
    public void setStorageUsed(Long storageUsed) { this.storageUsed = storageUsed; }
    public void setStorageLimit(Long storageLimit) { this.storageLimit = storageLimit; }

    /**
     * 验证组接口
     */
    public interface RegisterValidation {}
}
